package com.xinqi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinqi.dto.ClickUser;
import com.xinqi.dto.PageData;
import com.xinqi.dto.Result;
import com.xinqi.entity.Blog;
import com.xinqi.entity.User;
import com.xinqi.mapper.BlogMapper;
import com.xinqi.service.BlogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.xinqi.dto.PageData;

import java.util.List;

//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;

/**
 * @author xin麒
 * @date 2023/6/25 9:05
 */
@Slf4j
@RestController
@RequestMapping("/blog")
@Api("博客模块")
public class BlogController {
    @Autowired
    private BlogService blogService;

    @GetMapping("{page}/{limit}")
    public Result findPageQueryBook(@PathVariable Long page, @PathVariable Long limit) {
        //创建page对象
        Page<Blog> pageParam = new Page<>(page, limit);
        //调用service方法
        List<Blog> pageModel = blogService.getPageByLimited(pageParam);
        //返回
        return Result.ok(pageModel);
    }


    @PostMapping
    @ApiOperation("新增博客")
    public Result save(@RequestBody Blog blog) {
        blogService.save(blog);
        return Result.ok("新增blog成功!");
    }

    @GetMapping("/{id}")
    @ApiOperation("blog信息")
    public Result get(@PathVariable("id") Long id) {
        return Result.ok(blogService.getById(id));
    }


    @GetMapping("/likes/{id}")
    @ApiOperation("根据博客id查询该博客的点赞数量")
    public Result queryBlogLikesNumber(@PathVariable("id") Long id) {
        return Result.ok(blogService.queryBlogLikesNumber(id));
    }

    /**
     * 正常开发的话使用put是比较合适的，如果是post的话那就直接传一个blogId即可
     * 只能说是没完成登录功能的锅吧
     *
     * @param clickUser
     * @return
     */
    @PostMapping("/like")
    @ApiOperation("根据用户id对该博客进行点赞或取消点赞")
    public Result likeBlog(@RequestBody ClickUser clickUser) {
        // 修改点赞数量
        Long blogId = clickUser.getBlogId();
        Long userId = clickUser.getUserId();

        return blogService.likeBlog(blogId, userId);
    }


    @GetMapping("/getAllBlogLikesUsersByBlogId/{id}")
    @ApiOperation("根据博客id查询该博客的点赞数量")
    public Result getAllBlogLikesUsersByBlogId(@PathVariable("id") Long id) {
        List<User> users = blogService.getAllBlogLikesUsersByBlogId(id);
        return Result.ok(users);
    }

}
